package dyyx.echo;

import java.util.HashMap;
import java.util.Map;

import dyyx.util.NettyUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.concurrent.DefaultThreadFactory;

public class EchoServer {

	
    static final int port = Integer.parseInt(System.getProperty("port", "6789"));
    static final int bossThreads = Integer.parseInt(System.getProperty("bossThreads", "1"));
    static final int workerThreads = Integer.parseInt(System.getProperty("workerThreads", "3"));
    static final int bizThreads = Integer.parseInt(System.getProperty("bizThreads", "10"));
    
    static final String TYPE_bizThreadPool = "bizThreadPool";
	static final String TYPE_bizEventLoopGroup = "bizEventLoopGroup";

    // bizThreadPool 业务线程池 ， bizEventLoopGroup  使用单独的 EventLoopGroup  ，  default   都不使用
	static final String type = System.getProperty("type", TYPE_bizThreadPool);
    
	


	public static Map<String,Object> getInfo(){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("port", port);
		map.put("bossThreads", bossThreads);
		map.put("workerThreads", workerThreads);
		map.put("bizThreads", bizThreads);
		map.put("type", type);


		return map;
	}
	
	
	public static void main(String[] args) throws Exception {
		
		
		System.out.println("echo server start "+System.currentTimeMillis());
		System.out.println(getInfo());

        // Configure the server.
        //  NioEventLoopGroup(int nThreads) 
        // int DEFAULT_EVENT_LOOP_THREADS = Math.max(1, SystemPropertyUtil.getInt("io.netty.eventLoopThreads", Runtime.getRuntime().availableProcessors() * 2));
        // Runtime.getRuntime().availableProcessors() * 2
		

        EventLoopGroup bossGroup = new NioEventLoopGroup(bossThreads,new DefaultThreadFactory("bossGroup"));
        EventLoopGroup workerGroup = new NioEventLoopGroup(workerThreads,new DefaultThreadFactory("bossGroup"));
        // 单线程 方便 debug 
        // EventLoopGroup singleGroup = new NioEventLoopGroup(1);

        
        //  ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) 
        // the parent (acceptor) and the child (client)
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.option(ChannelOption.SO_BACKLOG, 1024);
            b.option(ChannelOption.TCP_NODELAY, true);
            b.childOption(ChannelOption.TCP_NODELAY, true);

            
            b.group(bossGroup, workerGroup)
            // b.group(singleGroup, singleGroup)
             .channel(NioServerSocketChannel.class)
             // 去掉这个 启动时不会打印日志
             .handler(new LoggingHandler(LogLevel.INFO))
             // .handler(new LoggingHandler(LogLevel.DEBUG))
             .childHandler(new EchoServerInit());

            Channel ch = b.bind(port).sync().channel();

            System.out.println("start done,port="+port);
    		System.out.println(getInfo());
    		System.out.println(NettyUtil.getInfo());

            
            ch.closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

		

	
}
